﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using PersonaLibrary.Data;
using System.Windows.Xps.Packaging;

namespace PersonaLibrary.ViewModel.PrintViewModel
{
    sealed internal class BithdayTemplate : BasePrintTemplate, Model.ITemplateConfigurable
    {
        private List<ConflictRow> conflictRows;
        private BithdayProperty property = new BithdayProperty();
        private RelayCommand showConfig;

        private ITemplateProperties ConfigView
        {
            get
            {
                return ((Model.ITemplateConfigurable) this).ConfigView;
            }
        }

        ITemplateProperties Model.ITemplateConfigurable.ConfigView
        {
            get
            {
                return property;
            }
        }

        public BithdayTemplate(string templatePath) 
            : base(templatePath)
        {
            conflictRows = new List<ConflictRow>();
        }

        public RelayCommand ShowConfig
        {
            get
            {
                if(showConfig == null)
                    showConfig = new RelayCommand(x => show(), x => !ConfigView.IsWindowOpened);
                return showConfig;
            }
        }

        private void show()
        {
            ConfigView.View.Show();
        }

        protected override IDocumentPaginatorSource CreateTable()
        {
            TableRowGroup rowGroup = null;
            rowGroup = Flow.FindName("contentRow") as TableRowGroup;

            Table table = Flow.FindName("contentTabel") as Table;

            
            rowGroup = table.RowGroups.Single(x => x.Name == "contentRow");
            
            if (rowGroup == null)
                return null;

            string birthDay = "";
            int tmpAge = 0;
            string age = "";

            for (int i = 1; i < Items.Count; i++)
            {
                TableRow row = new TableRow();
                
                row.Cells.Add(new TableCell(new Paragraph(new Run(i.ToString()))));
                row.Cells.Add(new TableCell(new Paragraph(new Run(Items[i].ToString()))));

                if (Items[i].Birthday.HasValue)
                {
                    birthDay = Items[i].Birthday.Value.ToString("dd.M.yyyy");
                    tmpAge = (DateTime.Now.Year - Items[i].Birthday.Value.Year + 1);
                    if (tmpAge != 0 && tmpAge != -1)
                        age = tmpAge.ToString();
                    else
                        age = string.Empty;
                }
                else
                {
                    birthDay = string.Empty;
                    age = string.Empty;
                }

                row.Cells.Add(new TableCell(new Paragraph(new Run(birthDay))));
                row.Cells.Add(new TableCell(new Paragraph(new Run(age))));

                if (Items[i].ConnectionJobEmplDeps.Count == 1)
                {
                    row.Cells.Add(new TableCell(new Paragraph(new Run(Items[i].ConnectionJobEmplDeps[0].Department.Organization.ToString()))));
                    row.Cells.Add(new TableCell(new Paragraph(new Run(Items[i].ConnectionJobEmplDeps[0].Job.Name))));
                    row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    if (string.IsNullOrEmpty(Items[i].PersonalAdress))
                        row.Cells.Add(new TableCell(new Paragraph(new Run(Items[i].ConnectionJobEmplDeps[0].Department.Address))));
                    else
                    {
                        ConflictRow conflictRow = new ConflictRow(Items[i]) { RowIndex = rowGroup.Rows.Count };
                        conflictRow.ConflictCells.Add(createDepartmentAdressConflictCells(Items[i], 7));
                        conflictRows.Add(conflictRow);
                        row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    }
                    row.Cells.Add(new TableCell(new Paragraph(new Run(Items[i].ConnectionJobEmplDeps[0].Department.Telephone))));
                }
                else
                {
                    ConflictRow conflictRow = new ConflictRow(Items[i]) {RowIndex = rowGroup.Rows.Count};

                    conflictRow.ConflictCells.Add(createDepartmentAdressConflictCells(Items[i], 7));
                    conflictRow.ConflictCells.Add(createOrganizationConflictCells(Items[i], 4));
                    conflictRow.ConflictCells.Add(createJobConflictCells(Items[i], 5));
                    conflictRows.Add(conflictRow);

                    row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    row.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                }

                rowGroup.Rows.Add(row);
            }
            
            return Flow;
        }

        private ConflictCell createOrganizationConflictCells(Employee employee, int columnIndex)
        {
            return new ConflictCell
                       {
                           AvailableValues =
                               new List<string>(
                               employee.ConnectionJobEmplDeps.Select(x => x.Department.Organization.ToString())),
                           Header = "Организация",
                           CellIndex = columnIndex
                       };
        }

        private ConflictCell createJobConflictCells(Employee employee, int columnIndex)
        {
            return new ConflictCell
                       {
                           AvailableValues = new List<string>(employee.ConnectionJobEmplDeps.Select(x => x.Job.Name)),
                           Header = "Должность",
                           CellIndex = columnIndex
                       };
        }

        private ConflictCell createDepartmentAdressConflictCells(Employee employee, int columnIndex)
        {
            ConflictCell result = new ConflictCell
                                      {
                                          AvailableValues =
                                              new List<string>(
                                              employee.ConnectionJobEmplDeps.Select(x => x.Department.Address)),
                                          Header = "Адрес",
                                          CellIndex = columnIndex
                                      };
            if (string.IsNullOrEmpty(employee.PersonalAdress))
                result.AvailableValues.Add(employee.PersonalAdress);
            return result;
        }
    }
}
